<?php defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * @author CHUN Dany <chundany@gmail.com>
 */
class Profile extends Admin_Controller
{
  function __construct()
  {
    parent::__construct();
    $this->load->model('mod_user');
  }
  
  public function edit()
  {
    $id = $this->session->userdata('userId');
    $row = $this->mod_user->getUserById($id);
    
    if($this->input->post())
    {
      $this->form_validation->set_rules('fname', 'First Name', 'trim|xss_clean|required|max_length[45]')
                            ->set_rules('lname', 'Last Name', 'trim|xss_clean|required|max_length[45]')
                            ->set_rules('email', 'Email', 'trim|xss_clean|required|valid_email|max_length[150]|uniqueUnless['.table('user').'.'.field('email').'.'.field('id').'.'.$id.']')
                            ->set_rules('phone', 'Phone', 'trim|xss_clean|max_length[45]')
                            ->set_rules('role', 'Role', 'trim|xss_clean|required|max_length[10]')
                            ->set_rules('disable', 'Disable', 'trim|xss_clean');
      
      if($this->form_validation->run())
      {       
        $config['upload_path'] = UPLOAD_PATH . 'users/';
        $config['allowed_types'] = 'gif|jpg|jpeg|png';
        $config['max_size'] = '512';
      
        $this->load->library('upload', $config);
    
        if($this->upload->do_upload('image'))
        {
          $image = $this->upload->data();
          $imageName = $image['file_name'];
          
          if($this->mod_user->update($id, $imageName) > 0)
          {
            resize_image($imageName, 250, 250, UPLOAD_PATH . 'users/', UPLOAD_PATH . 'users/');
            resize_image($imageName, 75, 75, UPLOAD_PATH . 'users/', UPLOAD_PATH . 'users/75x75/');
            
            delete_file(UPLOAD_PATH . 'users/' . $row[field('image')]);
            delete_file(UPLOAD_PATH . 'users/75x75/' . $row[field('image')]);

            $this->session->set_userdata('userPhoto', $imageName);
            successMsg('Successfully edited!');
            redirect('user-profile');
          }
          else
          {
            delete_file(UPLOAD_PATH . 'users/' . $imageName);
            errorMsg('Could not edit, please try again!');
          }
        }
        else if(!$this->upload->display_errors())
        {
          if($this->mod_user->update($id) > 0)
          {
            successMsg('Successfully edited!');
            redirect('user-profile');
          }
          else
          {
            errorMsg('Could not edit, please try again!');
          }
        }
        else
        {
          errorMsg($this->upload->display_errors());
        }
      }
    }
    $this->role = $this->mod_user->getRoleList();
    $this->row = $row;
      
    $this->title  = 'Edit User Profile';
    $this->action = 'Edit User Profile';
  }
  
  public function change_password(){
    $id = $this->session->userdata('userId');
    
    if($this->input->post())
    {
      $this->form_validation->set_rules('password', 'Password', 'trim|xss_clean|required|max_length[45]')
                            ->set_rules('conPassword', 'Confirm Password', 'trim|xss_clean|required|max_length[45]|matches[password]');
      
      if($this->form_validation->run())
      {           
        if($this->mod_user->changePassword($id) > 0)
        {
          successMsg('Successfully change user password!');
          redirect('change-password');
        }
        else
        {
          errorMsg('Password could not change, please try again!');
        }
      }
    }
      
    $this->title  = 'Change Password';
    $this->action = 'Change Password';
  }
  
  public function detail(){
    $id = $this->session->userdata('userId');
    $this->row = $this->mod_user->getUserById($id);
      
    $this->title  = 'User Profile';
    $this->action = 'User Profile';
  }
}